package be.ipl.streams.servlets.updaters;

import java.io.IOException;
import java.util.List;

import javax.ejb.EJB;
import javax.servlet.RequestDispatcher;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import be.ipl.streams.domaine.Game;
import be.ipl.streams.usecases.GameManagement;


@SuppressWarnings("serial")
@WebServlet("/updateParties.html")
public class GameList extends HttpServlet implements Servlet {
	
	@EJB
	private GameManagement gameManagement;
	
	@Override
	protected void doPost (HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
	
	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
		if (request.getSession().getAttribute("player") == null) return;
		if (request.getSession().getAttribute("game") != null) return;
		
		List<Game> parties = gameManagement.listLobbys();
		request.setAttribute("games", parties);
		RequestDispatcher rd = getServletContext().getNamedDispatcher("updateParties");
		rd.forward(request, response);
	}
	
}
